import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Settings".tr),
        backgroundColor: Colors.blue.shade300,
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text("Language".tr),
            trailing: const Icon(Icons.chevron_right, color: Colors.black54),
            onTap: () {
              _showLanguageDialog(context);
            },
          ),
          const Divider(
            color: Colors.grey,
          ),
        ],
      ),
    );
  }

  // 语言切换选择框
  void _showLanguageDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return const LanguageDialog();
      },
    );
  }
}

class LanguageDialog extends StatelessWidget {
  const LanguageDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Language'.tr),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ListTile(
            title: const Text('English'),
            trailing: Get.locale?.languageCode == 'en'
                ? const Icon(Icons.check, color: Colors.blue)
                : null,
            onTap: () {
              Get.updateLocale(const Locale('en', 'US'));
              Get.toNamed("/");
            },
          ),
          ListTile(
            title: const Text('简体中文'),
            trailing: Get.locale?.languageCode == 'zh'
                ? const Icon(Icons.check, color: Colors.blue)
                : null,
            onTap: () {
              Get.updateLocale(const Locale('zh', 'CN'));
              Get.toNamed("/");
            },
          ),
        ],
      ),
    );
  }
}
